package;
import flash.display.MovieClip;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.filters.BevelFilter;
import flash.filters.BitmapFilterQuality;
import flash.filters.BitmapFilterType;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.Lib;

@:bind class Panel extends MovieClip
{	
	public var inset_mc:MovieClip;
	public var title_tf:TextField;
	public var bg_mc:MovieClip;
	public var close_btn:SimpleButton;
	public var print_btn:SimpleButton;
	public var resize_btn:SimpleButton;
	
	var minWidth:Float;
	var minHeight:Float;
	var w:Float;
	var h:Float;
	
	public function new()
	{
		super();
		minWidth=100;
		minHeight=100;

		bg_mc.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
		bg_mc.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		bg_mc.buttonMode = true;
		
		resize_btn.addEventListener(MouseEvent.MOUSE_DOWN, onResizeStart);
		close_btn.addEventListener(MouseEvent.CLICK, onClose);
		
		title_tf.autoSize=TextFieldAutoSize.CENTER;
		
		onColorChange(0x0099FF,false);
		print_btn.visible=false;
		//updateLayout();
	}
	function onStartDrag(event:MouseEvent):Void
	{
		parent.setChildIndex(this, parent.numChildren-1);
		startDrag();
	}
	function onStopDrag(event:MouseEvent):Void
	{
		stopDrag();
	}
	function onResizeStart(event:MouseEvent):Void
	{
		stage.addEventListener(MouseEvent.MOUSE_MOVE, onResize);
		stage.addEventListener(MouseEvent.MOUSE_UP, onResizeStop);
	}
	function onResize(event:MouseEvent):Void
	{
		setSize(stage.mouseX-this.x+15, stage.mouseY-this.y+15);
	}
	function onResizeStop(event:MouseEvent):Void
	{
		stage.removeEventListener(MouseEvent.MOUSE_UP, onResizeStop);
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, onResize);
	}
	public function setSize(w, h)
	{
		this.w = w;
		this.h = h;
		if(w<minWidth)	this.w = minWidth;
		if(h<minHeight)	this.h = minHeight;
		updateLayout();
	}
	public function setTitle(s:String)
	{
		title_tf.text=s;
		title_tf.x = ( w - title_tf.width ) / 2;
	}
	public function getTitle():String
	{
		return title_tf.text;
	}
	function updateLayout()
	{
		bg_mc.width=w;
		bg_mc.height=h;

		title_tf.x = ( w - title_tf.width ) / 2;
		close_btn.x = w - 10 - close_btn.width;
		
		resize_btn.x = w - 5 - resize_btn.width;
		resize_btn.y = h - 5 - resize_btn.height;

		inset_mc.bg.width = w - 2*28 + 20;
		inset_mc.bg.height = h - 2*55 + 20;

		print_btn.y = h - print_btn.height - 10;
	}
	function onColorChange(insetColor, noColor)
	{
		inset_mc.filters=[];
		var bevelFilter:BevelFilter = new BevelFilter(
							  2,
							  45,
							  0x333333, 1.0,
							  0xFFFFFF, 1.0,
							  3, 3, 1.0,
							  BitmapFilterQuality.HIGH,
							  BitmapFilterType.INNER);
		if(noColor)
		{
			bevelFilter.knockout=true;
		}
		else
		{
			var t = inset_mc.bg.transform.colorTransform;
			t.color=insetColor;
			inset_mc.bg.transform.colorTransform = t;
		}
		inset_mc.filters=[bevelFilter];
	}
	function onClose(event:MouseEvent):Void
	{
		parent.removeChild(this);
	}
}